﻿@model CurrencyModel
@using Telerik.Web.Mvc.UI;
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
@Html.Telerik().TabStrip().Name("currency-edit").Items(x =>
{
    x.Add().Text(T("Admin.Configuration.Currencies.Info").Text).Content(TabInfo().ToHtmlString());
    x.Add().Text(T("Admin.Configuration.Currencies.Stores").Text).Content(TabStores().ToHtmlString());

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "currency-edit"));
}).ClientEvents(events => events.OnSelect("telerik_on_tab_select")).SetSelectedTabIndex(GetSelectedTabIndex())

@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
{
    @(Html.LocalizedEditor<CurrencyModel, CurrencyLocalizedModel>("currency-info-localized",
                @<table class="adminContent">
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Locales[item].Name):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => Model.Locales[item].Name)
                            @Html.ValidationMessageFor(model => model.Locales[item].Name)
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            @Html.HiddenFor(model => model.Locales[item].LanguageId)
                        </td>
                    </tr>
                </table>
                ,
                @<table class="adminContent">
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Name):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => model.Name)
                            @Html.ValidationMessageFor(model => model.Name)
                        </td>
                    </tr>
                </table>
            ))
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CurrencyCode):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CurrencyCode)
                @Html.ValidationMessageFor(model => model.CurrencyCode)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Rate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Rate)
                @Html.ValidationMessageFor(model => model.Rate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DisplayLocale):
            </td>
            <td class="adminData">
                @{
                    var cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.SpecificCultures)
                        .OrderBy(x => x.EnglishName)
                        .Select(x => new SelectListItem()
                                         {
                                             Value = x.IetfLanguageTag,
                                             Text = string.Format("{0}. {1}", x.EnglishName, x.IetfLanguageTag)
                                         })
                        .ToList();
                    cultures.Insert(0, new SelectListItem()
                                           {
                                             Value = "",
                                             Text = @T("Admin.Configuration.Currencies.Fields.DisplayLocale.Select").Text
                                           });
                }
                @Html.DropDownListFor(model => model.DisplayLocale, cultures)
                @Html.ValidationMessageFor(model => model.DisplayLocale)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CustomFormatting):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CustomFormatting)
                @Html.ValidationMessageFor(model => model.CustomFormatting)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Published):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Published)
                @Html.ValidationMessageFor(model => model.Published)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DisplayOrder):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DisplayOrder)
                @Html.ValidationMessageFor(model => model.DisplayOrder)
            </td>
        </tr>
        @if (Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.CreatedOn):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(model => model.CreatedOn)
                </td>
            </tr>
        }
    </table>
}

@helper TabStores()
{
    <script type="text/javascript">
        $(document).ready(function () {

            $("#@Html.FieldIdFor(model => model.LimitedToStores)").click(toggleStoreMapping);
        
            toggleStoreMapping();
        });


        function toggleStoreMapping() {
            if ($('#@Html.FieldIdFor(model => model.LimitedToStores)').is(':checked')) {
                $('#pnl-available-stores').show();
            }
            else {
                $('#pnl-available-stores').hide();
            }
        }

    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.LimitedToStores):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.LimitedToStores)
                @Html.ValidationMessageFor(model => model.LimitedToStores)
            </td>
        </tr>
        <tr id="pnl-available-stores">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AvailableStores):
            </td>
            <td class="adminData">
                @if (Model.AvailableStores != null && Model.AvailableStores.Count > 0)
                {
                    foreach (var store in Model.AvailableStores)
                    {
                    <div>
                        <input type="checkbox" name="SelectedStoreIds" value="@store.Id" checked="@(Model.SelectedStoreIds != null && Model.SelectedStoreIds.Contains(store.Id))" />@store.Name
                    </div>
                    }
                }
                else
                {
                    <div>No stores defined</div>
                }</td>
        </tr>
    </table>
}
